package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.pojo.Paitient;
import com.xie.pojo.Personnel;
import com.xie.service.PersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "员工管理")
@RestController
@RequestMapping("/Personnels")
public class PersonnelController {
    @Autowired
    private PersonnelService personnelService;

    /**
     * 分页
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("{pageIndex}/{pageSize}")
    @ApiOperation("获取分页员工数据")
    public Result findPersonnel(@PathVariable Integer pageIndex, @PathVariable Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        Page page1 = personnelService.page(page);
        if (page1 != null) return Result.ok();
        return Result.fail();
    }

    /**
     * 模糊查询
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询员工信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Personnel> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Personnel::getEmpBirthday,params)
                .or().like(Personnel::getEmpCountry,params)
                .or().like(Personnel::getEmpDeptId,params)
                .or().like(Personnel::getEmpDuty,params)
                .or().like(Personnel::getEmpEmail,params)
                .or().like(Personnel::getEmpGender,params)
                .or().like(Personnel::getEmpHealth,params)
                .or().like(Personnel::getEmpHomeAddress,params)
                .or().like(Personnel::getEmpHometown,params)
                .or().like(Personnel::getEmpId,params)
                .or().like(Personnel::getEmpIdCard,params)
                .or().like(Personnel::getEmpJobId,params)
                .or().like(Personnel::getEmpMarriage,params)
                .or().like(Personnel::getEmpName,params)
                .or().like(Personnel::getEmpNation,params)
                .or().like(Personnel::getEmpNo,params)
                .or().like(Personnel::getEmpStartWork,params)
                .or().like(Personnel::getEmpState,params)
                .or().like(Personnel::getEmpTeleNo,params)
                .or().like(Personnel::getEmpXl,params)
        ;
        List<Personnel> list = personnelService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加管理员信息
     */
//    @RequestMapping(value = "/addPersonnel" ,method = RequestMethod.POST)
//    @ResponseBody
    @PostMapping("/save")
    @ApiOperation("添加员工")
    public Result addPersonnel(@RequestBody Personnel personnel) {
        boolean save = personnelService.save(personnel);
        if (save) return Result.ok();
        return Result.fail();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除员工")
    public Result deletePersonnel(@PathVariable Integer id) {
        boolean b = personnelService.removeById(id);
        if (b)return Result.ok();
        return Result.fail();
    }


    /**
     * 修改员工
     * @param personnel
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改员工信息")
    public Result updatePersonnel(@RequestBody Personnel personnel) {
        boolean update = personnelService.updateById(personnel);
        if (update)return Result.ok();
        return Result.fail();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工信息")
    @GetMapping("/findById/{id}")
    public Result findPersonnelById(@PathVariable Integer id) {
        Personnel pe2= personnelService.getById(id);
        if (pe2 != null)return Result.ok();
        return Result.fail();
    }


    /**
     * 获取所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取所有员工信息")
    public Result exportPersonnel(){
        List<Personnel> personnel = personnelService.list();
        if (personnel!= null)return Result.ok(personnel);
        return Result.fail();
    }
}
